package rjeutsky.xml.common;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import javax.xml.XMLConstants;
import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import rjeutsky.models.CardType;
import rjeutsky.models.OldCard;
import rjeutsky.xml.OldCardFileSerializer;

public abstract class OldCardFileWriter implements OldCardFileSerializer {

	protected String filePath;
	
	protected List<OldCard> cards;
	
	@Override
	public String getFilePath() {
		return filePath;
	}

	@Override
	public boolean serializeToFile(){
		try {
			File file = new File(filePath);
			XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(new FileWriter(file));
			writeDocument(writer);
			return true;
		} catch (FactoryConfigurationError e) {
			System.err.println("xml output factory configuration error");
		} catch (XMLStreamException e) {
			System.err.println("xml stream error");
		} catch (IOException e) {
			System.err.println("general io error");
		}
		return false;
	}

	@Override
	public void setFilePath(String path) {
		filePath = path;
	}

	@Override
	public void setOldCardList(List<OldCard> list) {
		cards = list;
	}
	
	protected void writeDocument(XMLStreamWriter writer) throws XMLStreamException {
		writer.writeStartDocument();
		writer.writeStartElement(TagNames.OLD_CARD_COLLECTION);
		writer.writeAttribute("xmlns:xsi", XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
		writer.writeAttribute("xsi:noNamespaceSchemaLocation", Constants.SCHEMA_PATH);
		for (OldCard card : cards) {
			writeCard(writer, card);
		}
		writer.writeEndDocument();
		writer.close();
	}
	
	protected void writeCard(XMLStreamWriter writer, OldCard card) throws XMLStreamException {
		writer.writeStartElement(TagNames.OLD_CARD);
		writeStringElement(writer, TagNames.THEMA, card.getThema());
		writeCardTypeElement(writer, card.getType());
		writeStringElement(writer, TagNames.COUNTRY, card.getCountry());
		writeStringElement(writer, TagNames.YEAR, card.getYear());
		if (card.getAuthor() != null) {
			writeStringElement(writer, TagNames.AUTHOR, card.getAuthor());
		}
		if (card.getValuable() != null) {
			writeStringElement(writer, TagNames.VALUABLE, card.getValuable());
		}
		writer.writeEndElement();
	}
	
	protected <T> void writeStringElement (XMLStreamWriter writer, String name, T value) throws XMLStreamException {
		writer.writeStartElement(name);
		writer.writeCharacters(String.valueOf(value));
		writer.writeEndElement();
	}
	
	protected void writeCardTypeElement(XMLStreamWriter writer, CardType type) throws XMLStreamException{
		writer.writeStartElement(TagNames.CARD_TYPE);
		writer.writeAttribute(TagNames.IS_SEND, String.valueOf(type.getIsSent()));
		writer.writeCharacters(type.getType().name().toUpperCase());
		writer.writeEndElement();
	}
}
